import Dic from "@/utils/dic";
import {ElMessage} from "element-plus";
import saveAs from "file-saver"
import {h} from "vue";

export default {
    data() {
        return {
            Dic,
            currentShop: window.localStorage.getItem("currentShop")
        }
    },
    methods: {
        tableRed(row, column, value) {
            return h(ElText, {type: value > 0 ? 'danger' : 'info',}, {default: () => value})
        },
        tablePrice(row, column, value) {
            return h(ElText, {type: value > 0 ? 'success' : 'danger'}, {default: () => value})
        },
        /* 格式化人民币 */
        formatRMB(num) {
            if (isNaN(num)) {
                return '¥0.00';
            }
            // 转换为数字类型
            const number = Number(num);
            // 处理正负号
            const sign = number < 0 ? '-' : '';
            // 取绝对值并格式化为两位小数
            const absoluteValue = Math.abs(number);
            const formattedValue = absoluteValue.toFixed(2);
            // 返回格式化结果
            return sign + '¥' + formattedValue;
        },
        /* 表格导出流处理 */
        async exportHandle(res, title) {
            if (res.code === 500) {
                return ElMessage.warning(res.message)
            }
            if (res.type === 'application/json') {
                let data = JSON.parse(await res.text());
                return ElMessage.warning(data.message || "导出失败，请重试")
            }
            const dataBlob = new Blob([res]);
            saveAs(dataBlob, title);
            return ElMessage.success("导出成功，请查看下载文件")
        },
        isMobilePhone(value) {
            return /^1(3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$/.test(value)
        }
    }
}